import axios from "axios";

const myAxios = axios.create({
  baseURL: "http://localhost:8101",
  timeout: 60000,
  withCredentials: true,
});
const whiteList = [
  "/api/user/getToken",
  "/api/user/register",
  "/api/app/list/page/vo",
];
// 全局请求拦截器
myAxios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    if (!whiteList.some((url) => config.url?.includes(url))) {
      const token = localStorage.getItem("token");
      if (token) {
        config.headers.Authorization = `Bearer ${token}`;
      }
    }
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// 全局响应拦截器
myAxios.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    window.location.href = "/user/login";

    return Promise.reject(error);
  }
);

export default myAxios;
